package org.liurq.springcloud.controller;

import cn.hutool.core.util.IdUtil;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.liurq.springcloud.service.OrderHystrixService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@Slf4j
@DefaultProperties(defaultFallback = "paymentInfo_GlobalHandler")
public class OrderHystrixController {
    @Resource
    private OrderHystrixService orderHystrixService;

    @GetMapping("/consumer/payment/hystrix/ok/{id}")
    public String paymentinfo_OK(@PathVariable("id") Integer id) {
        String res = orderHystrixService.paymentinfo_OK(id);
        log.info("order:" + res);
        return res;
    }

    @GetMapping("/consumer/payment/hystrix/timeout/{id}")
    @HystrixCommand(fallbackMethod = "paymentInfo_TimeoutHandler", commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1000")
    })
//    @HystrixCommand
    public String paymentinfo_timeout(@PathVariable("id") Integer id) {
//        int a = 10/0;
        String res = orderHystrixService.paymentinfo_timeout(id);
        log.info("order:" + res);
        return res;
    }


    public String paymentInfo_TimeoutHandler(Integer id) {
        return "线程池：" + Thread.currentThread().getName() + " 80 paymentInfo_TimeoutHandler id:" + id;
    }

    public String paymentInfo_GlobalHandler() {
        return "线程池：" + Thread.currentThread().getName() + " 80 paymentInfo_GlobalHandler ";
    }

}
